Every virtual host is IP-based: sorts traffic to virtual hosts based on IP address client connected to
If multiple virtual hosts are declared for single IP/port combination, ServerName and ServerAlias directives are consulted
This effectively enables name-based virtual hosting
Wildcards and Priority
IP address part of <VirtualHost> directive can be replaced with wildcards _default_ and *
Both mean "match anything"
When HTTP request comes in:
httpd first tries to match against virtual hosts with explicit IP address
If no match, checks virtual hosts with wildcard IP address
If no match, main server configuration is used
Virtual Hosts
<VirtualHost *:80>
<VirtualHost *:80> always matches regular HTTP traffic on port 80/TCP
Effectively disables main server configuration for traffic on port 80/TCP
If no match found for ServerName or ServerAlias, and multiple virtual hosts are defined for IP/port combination request came in on, first virtual host that matches IP/port is used
"First" determined by order in which hosts are defined in configuration file
Multiple *.conf files are included in alphanumeric order
To create catch-all (default) virtual host, name configuration file something like 00-default.conf
Virtual Hosts
Troubleshooting Virtual Hosts
Configure separate DocumentRoot for each virtual host, with identifying content
Configure separate log files for error logging and access logging
Evaluate order in which virtual host definitions are parsed by httpd
Files are read in alphanumeric order based on filename
Disable virtual hosts one by one to isolate a problem
Comment out virtual host definitions in configuration file(s)
Temporarily rename include files to not end in .conf
Use journalctl UNIT=httpd.service to isolate log messages from just httpd.service
References
httpd(8) man page
httpd-manual package contents
HTTPS
Transport Layer Security (TLS)
TLS used to encrypt network communications
Successor to Secure Sockets Layer (SSL)
Allows client to verify identity of server, and server to verify client
Based on certificates
Certificate has multiple parts:
Public key
Server identity
Signature from certificate authority
Corresponding private key is never made public
Any data encrypted with private key can be decrypted only with public key, and vice versa
HTTPS
TLS Handshake
During initial handshake:
Set up encrypted connection
Client and server agree on set of encryption ciphers supported by both
Client and server exchange bits of random data
Client uses random data to generate session key
Key used for faster symmetric encryptionsame key used for both encryption and decryption
To secure key, it is sent to server encrypted with server’s public key (part of server certificate)
HTTPS
HTTPS
Configuring TLS certificates
Obtain signed certificate
Install Apache HTTPD extension modules to support TLS
Configure virtual TLS host
Obtaining a Certificate
Two options:
Create self-signed certificate (signed by itself, not Certificate Authority)
Create certificate request
Have reputable CA sign request so it becomes a certificate
crypto-utils package contains genkey utility that supports both methods
HTTPS
To create certificate (signing request) with genkey:
[root@server1 ~]# genkey <FQDN>
<FQDN> is fully qualified domain name clients use to connect to server
genkey asks following:
Desired key size: Choose at least 2048 bits
Should signing request be made: Answering no creates self-signed certificate
Should private key be protected with passphrase
Identity of server
HTTPS
After certificate process completes, several files are created:
/etc/pki/tls/private/<fqdn>.key: Private key
Keep at 0600 or 0400 permissions
SELinux context of cert_t
Do not share
/etc/pki/tls/certs/<fqdn>.0.csr: Generated if signing request created
Send to CA to be signed
Never need to send private key to CA
/etc/pki/tls/certs/<fqdn>.crt: Public certificate
Returned from the CA when self-signed certificate requested
Keep permissions at 0644 with SELinux context of cert_t
HTTPS
Installing Apache HTTPD Modules
Apache HTTPD needs extension module to activate TLS support
Install using mod_ssl package
mod_ssl enables httpd for default virtual host listening on port 443/TCP
Host configured in /etc/httpd/conf.d/ssl.conf
Configuring Virtual Host with TLS
Configure TLS virtual hosts same as regular virtual hosts with additional parameters
Can use name-based virtual hosting with TLS, but some older browsers are not compatible
If signed certificate used, and certificate does not have embedded copies of all certificates used in signing up to root CA, server must provide certificate chain
Copy of all CA certificates used in signing process concatenated together
To identify file, use SSLCertificateChainFile directive
When defining new TLS-encrypted virtual host, do not need to copy entire contents of ssl.conf
Need <VirtualHost> block with SSLEngine On directive
Example is missing important directives such as DocumentRoot
Are inherited from main configuration
Not defining protocols and ciphers to use results in httpd using default options. httpd defaults are not considered secure. It is highly recommended to restrict both to a more secure subset.
HTTPS
Configuring Forward Secrecy
If server private key is compromised (server break-in, crypto code bug), attacker could decrypt recorded session
Protecting against these types of attacks is called ensuring forward secrecy
To establish forward secrecy:
Carefully tune allowed ciphers in SSLCipherSuite directive
Have server always select most preferred cipher that both server and client support
List prioritizes ciphers that perform initial session key exchange using elliptic curve Diffie-Hellman (EECDH) algorithms
Session key is never transmitted; it is calculated by both sides
SSLHonorCipherOrder On directive (last line) instructs httpd to prefer ciphers listed earlier in SSLCipherSuite list, regardless of client preference
Security research is always-ongoing
Re-evaluate selected ciphers on regular basis
HTTPS
Configuring HTTP Strict Transport Security (HSTS)
Common misconfiguration, which results in warnings in most browsers, is having web page served out over HTTPS include resources served out over clear-text HTTP
To protect against this, add line inside <VirtualHost> block that has TLS enabled
Header always set Strict-Transport-Security "max-age=15768000"
Sending extra header informs clients they are not allowed to fetch resources for this page that are not served using TLS
Another issue is clients connecting over HTTP to resource they should have used HTTPS for
Not serving content over HTTP solves issue
More subtle approach is to redirect clients connecting over HTTP to the same resource using HTTPS
HTTPS
To set up redirect, configure http virtual host for same ServerName and ServerAlias as TLS-protected virtual host
Catch-all virtual host can be used
Add following lines inside <VirtualHost *:80> block:
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_POST}$1 [redirect=301]
RewriteEngine on directive turns on URL rewrite module for virtual host
RewriteRule matches any resource (^(/.*)$) and redirects it using HTTP Moved Permanently message ([redirect=301]) to same resource served out over HTTPS
%{HTTP_HOST} variable uses hostname requested by client, while $1 part is back-reference to whatever was matched between first set of parentheses in regular expression
CGI is one of oldest forms of generating dynamic content
When CGI resource is requested, httpd executes resource as process and serves the stdout of that process
CGI resources mostly written in scripting languages like Perl
C programs and Java executables also common
Environment variables used to make request information (including client info) available to CGI program
Dynamic Web Content
Configuring httpd for CGI
To have httpd treat location as CGI executables, use following syntax in httpd configuration
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
Instructs httpd to redirect requests for files under /cgi-bin/ URI to /var/www/cgi-bin/ directory and treat files in that directory as executable scripts
CGI scripts:
Are executed as apache user and group
Should be executable by apache user and group
Should have httpd_sys_script_exec_t SELinux context
CGI directory:
Should have Options None
Should have access granted using normal <Directory> block
Dynamic Web Content
Serving Dynamic PHP Content
PHP scripting language is popular way to provide dynamic content
Performance and security improved by having httpd run PHP interpreter internally
php package adds special mod_php module httpd
Default configuration for mod_php adds lines to httpd
<FilesMatch> block instructs httpd to use mod_php for file names ending in .php
DirectoryIndex directive adds index.php to list of files searched when directory is requested
Dynamic Web Content
Serving Dynamic Python Content
Python scripts also popular for dynamic content
python and httpd support newer Web Server Gateway Interface (WSGI) protocol
mod_wsgi package adds WSGI support to httpd
Unlike mod_php or CGI, WSGI does not start new script/interpreter for every request
Main application is started, and all requests are routed to that application
To configure httpd to support WSGI application:
Install mod_wsgi package
Add WSGIScriptAlias line to virtual host definition
Dynamic Web Content
Example: Send all requests for http://servername/myapp and any resources below it to WSGI application /srv/myapp/www/myapp.py
WSGIScriptAlias /myapp/ /srv/myapp/www/myapp.py
WSGI applications should:
Be executable by apache user and group
Have SELinux contexts set to httpd_sys_content_t
Dynamic Web Content
Database Connectivity
Most web applications store and retrieve persistent data
Common approach is database such as MariaDB or PostgreSQL
When database runs on same host as web server and uses standard network port, SELinux allows network connection from web application
When database runs on remote host, set SELinux Boolean httpd_can_network_connect_db to 1 to allow connection
When network connection needed from within web application, and target is not well-known database port, set SELinux Boolean httpd_can_network_connect to 1 to allow connection
Other SELinux Booleans also affect how web applications are executed by httpd
References
httpd(8) and httpd_php_selinux(8) man pages
httpd-manual package contents
/usr/share/doc/mod_wsgi-*/README
Summary
Apache HTTPD
Virtual Hosts
HTTPS
Dynamic Web Content
Module Completion
Nice job!
Click the button below to complete this module of the course:
Click the button below to continue to the course homepage: